﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using System.Reflection;

namespace Microhard.Core.Core
{
    public class WorkFlowUnity
    {
        public static string GetWorkFlowKeyName(object e, string methodName)
        {
            string rtn = null;
            if (e == null)
                return rtn;
            Type t = e.GetType();
            var addMethod = t.GetMethod(methodName);
            if (addMethod != null)
            {
                object[] flowAttr = addMethod.GetCustomAttributes(false);
                if (flowAttr != null && flowAttr.Length > 0)
                {
                    var atr = flowAttr[0] as MicrohardFlowAttr;
                    if (atr != null)
                    {
                        rtn = atr.ApproveFlowKeyName;
                    }
                }
            }
            return rtn;
        }

        public static ExecutedResult ExecuteRepositoryMethod<T>(T model, IRepository<T> target, string busMethodName)
        {        
            //取得方法            
            Type type = target.GetType();
            MethodInfo methodInfo = type.GetMethod(busMethodName, BindingFlags.Instance | BindingFlags.Public);
            if (methodInfo == null)
            {
                ExecutedResult rtn = new ExecutedResult();
                rtn.ErrorMsg.Add("methodInfo", "方法为空");
                return rtn;
            }
            //调用
            return (methodInfo.Invoke(target, new object[] { model }) as ExecutedResult);

        }
    }
}
